package com.example.hzz.criminalintent;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import java.text.SimpleDateFormat;
import java.util.UUID;

/**
 * Created by huangzezhan on 2018/8/12.
 */

public class CrimeFragment extends Fragment {
    private static final String TAG = "CrimeFragment";
    private Crime mCrime;
    private EditText mEdit;
    private Button mDateButton;
    private CheckBox mSolveCheckBox;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        UUID uid = (UUID) getActivity().getIntent().getSerializableExtra("UUID");
//        if (uid != null) {
//            Log.i(TAG, "onCreate uid=" + uid);
//            mCrime = CrimeLab.getInstance(getActivity()).getCrime(uid);
//        } else {
//            mCrime = new Crime();
//        }
        UUID uid = (UUID)getArguments().getSerializable("UUID");
        mCrime = CrimeLab.getInstance(getActivity()).getCrime(uid);
    }

    public static CrimeFragment newInstance(UUID uid){
        Bundle mBundle = new Bundle();
        mBundle.putSerializable("UUID",uid);

        CrimeFragment crime = new CrimeFragment();
        crime.setArguments(mBundle);
        return crime;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragement_crime, container, false);
        mDateButton = v.findViewById(R.id.date_id);
        mDateButton.setText(mCrime.getmDate().toString());
        mDateButton.setClickable(true);

        mSolveCheckBox = v.findViewById(R.id.solve_id);
        mSolveCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mCrime.setmSolved(b);
            }
        });

        Log.i(TAG, "onCreate title=" + mCrime.getmTitle());
        mDateButton.setText(mCrime.getmDate().toString());
        mSolveCheckBox.setChecked(mCrime.ismSolved());

        mDateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                DateFormat mDate = new SimpleDateFormat("E, M D, Y");
//                DateFormat
                SimpleDateFormat bf = new SimpleDateFormat("E, M d, Y");
                String mDate = bf.format(mCrime.getmDate());
                mDateButton.setText(mDate);
            }
        });

        mEdit = v.findViewById(R.id.title_id);
        mEdit.setText(mCrime.getmTitle());
        mEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mCrime.setmTitle(charSequence.toString());
                Log.i(TAG, "mEdit is change " + mCrime.getmTitle());
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        return v;
    }
}
